import userapi from '../../config/api/userapi' // 引入api接口管理文件
const dayjs = require("./dayjs");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    meetingList: [],
    carList: [],
    driverlist: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadData();
  },
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  loadData() {
    let vm = this;
    wx.showLoading({
      title: '加载中...',
    })
    //加载预约列表
    userapi.getAppointmentRecord({
      "openId": wx.getStorageSync('openid'),
    }, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      let list = res.data.data;
      //筛选会议室预约
      let meetingList = [];
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        if (item.type == 1) {
          //处理预约时间显示
          let date = dayjs(item.startTime.replace(/-/g, "/")).format('YY/MM/DD HH:mm');
          date += dayjs(item.endTime.replace(/-/g, "/")).format('-HH:mm');
          item.yydate = date;
          //判断结束时间是否已过
          let now = dayjs();
          let end = dayjs(item.endTime.replace(/-/g, "/"));
          if (end.isBefore(now)) {
            item.isover = true;
          } else {
            item.isover = false;
          }
          meetingList.push(list[i]);
        }

      }
      //筛选车辆预约
      let carList = [];
      let driverlist = [];
      for (let i = 0; i < list.length; i++) {
        let item = list[i];

        if (item.type == 2) {
          //处理预约时间显示
          let date = dayjs(item.startTime.replace(/-/g, "/")).format('YY/MM/DD HH:mm');
          date += dayjs(item.endTime.replace(/-/g, "/")).format('-HH:mm');
          item.yydate = date;
          //判断结束时间是否已过
          let now = dayjs();
          let end = dayjs(item.endTime.replace(/-/g, "/"));
          if (end.isBefore(now)) {
            item.isover = true;
          } else {
            item.isover = false;
          }
          carList.push(list[i]);
          if (item.suser) {
            driverlist.push(item);
          }
        }
      }


      //根据开始时间倒序
      meetingList.sort(function (a, b) {
        return new Date(b.startTime.replace(/-/g, "/")) - new Date(a.startTime.replace(/-/g, "/"));
      })
      carList.sort(function (a, b) {
        return new Date(b.startTime.replace(/-/g, "/")) - new Date(a.startTime.replace(/-/g, "/"));
      })
      driverlist.sort(function (a, b) {
        return new Date(b.startTime.replace(/-/g, "/")) - new Date(a.startTime.replace(/-/g, "/"));
      })


      vm.setData({
        meetingList: meetingList,
        carList: carList,
        driverlist: driverlist
      })
      console.log(list);
    })
  },
  clickitem(e) {
    let item = e.currentTarget.dataset.item;
    let jsstr = JSON.stringify(item);
    console.log(jsstr);
    jsstr = encodeURIComponent(jsstr);
    wx.navigateTo({
      url: '/pages/vehiclemanagement/vehiclemanagementinfo/vehiclemanagementinfo?infoitem=' + jsstr,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})